package com.sbpm.crmbpm.client.forms.controls;

import com.sbpm.crmbpm.client.application.dict.Dictionaries;
import com.sbpm.crmbpm.shared.data.dict.City;
import com.sbpm.crmbpm.shared.data.dict.Country;
import com.sencha.gxt.cell.core.client.form.ComboBoxCell;
import com.sencha.gxt.data.shared.LabelProvider;
import com.sencha.gxt.widget.core.client.form.PropertyEditor;
import com.sencha.gxt.widget.core.client.form.SimpleComboBox;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CountryComboBox extends SimpleComboBox< Country >
{

    private Map<String, Country> countries = new HashMap<String, Country>();

    public CountryComboBox()
    {
        super( new LabelProvider< Country >()
        {
            @Override
            public String getLabel( Country item )
            {
                return item.getName();
            }
        } );

        this.countries = Dictionaries.COUNTRIES;

        setClearValueOnParseError( false );
        setPropertyEditor( new PropertyEditor< Country >()
        {

            @Override
            public Country parse( CharSequence text ) throws ParseException
            {
                return countries.get( text.toString() );
            }

            @Override
            public String render( Country object )
            {
                return object == null ? "" : object.getName();
            }
        } );

        setTriggerAction( ComboBoxCell.TriggerAction.ALL );
        add( new ArrayList( countries.values() ) );
    }
}
